package com.focus.pay.wx.model.order.v3;

import cn.hutool.core.annotation.PropIgnore;
import cn.hutool.core.lang.Assert;
import cn.hutool.http.Method;
import cn.hutool.json.JSONUtil;
import com.focus.pay.wx.config.WxPayConfig;
import com.focus.pay.wx.constant.WxPayConstant;
import com.focus.pay.core.domain.PayModelDomain;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;
import lombok.Data;
import com.focus.pay.wx.utils.WxPayUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
public class WxPayCommon extends PayModelDomain {

    /**
     * 微信分配的公众账号ID
     */
    private String appid;

    /**
     * 微信支付分配的商户号
     */
    private String mch_id;


    /**
     * 默认SHA256-RSA
     */
    @PropIgnore
    private String signType;

    /**
     * 秘钥
     */
    @PropIgnore
    private String key;


    /**
     * 构建微信支付参数
     * @param <T>  支付类
     * @param wxPayConfig 微信配置
     * @return 请求属性
     */
    public <T extends WxPayCommon> T buildParam(T t, WxPayConfig wxPayConfig){
        t.setAppid(wxPayConfig.getAppId());
        t.setMch_id(wxPayConfig.getMchId());
        t.setSignType(Objects.isNull(wxPayConfig.getSignType())? WxPayConstant.WxPayEncryptionType.SHA256_RSA.toString().replace("_","-"):wxPayConfig.getSignType());
        t.setKey(wxPayConfig.getKey());
        return t;
    }

    /**
     * 构建请求头
     * @param requestMethod 请求方法
     * @param requestPath 请求路径
     * @param t 请求实体类
     * @param wxPayConfig 配置参数
     * @return 请求head参数
     */
    public <T extends WxPayCommon> Map<String,String> buildRequestHead(Method requestMethod, String requestPath, T t, WxPayConfig wxPayConfig) throws PayException {
        //参数校验
        Assert.notEmpty(wxPayConfig.getSerialNo(),() -> new PayException(ExceptionEnum.SERIALNO_EXCEPTION));
        Map<String,String> requestHead = new HashMap<>(); String prefix = "\n";
        //设置请求头
        requestHead.put("Content-Type","application/json");
        requestHead.put("Accept","application/json");
        //获取签名
        StringBuffer stringBufferSign = new StringBuffer()
                .append(requestMethod.toString()).append(prefix)
                .append(requestPath).append(prefix)
                .append(new Date().getTime()).append(prefix)
                .append(WxPayUtils.buildNonceStr()).append(prefix);
        if(Method.GET == requestMethod)
            stringBufferSign.append(prefix);
        else if(Method.POST == requestMethod){
            stringBufferSign.append(JSONUtil.toJsonStr(t)).append(prefix);
        }
        String sign = WxPayUtils.SHA256RSAEncryption(stringBufferSign.toString(),t.getKey());
        //设置认证参数
        String auth = "WECHATPAY2-SHA256-RSA2048mchid=\"%s\",serial_no=\"%s\",nonce_str=\"%s\",timestamp=\"%s\",signature=\"%s\"";
        String.format(auth,wxPayConfig.getMchId(),WxPayUtils.buildNonceStr(),new Date().getTime(),sign);
        requestHead.put("Authorization",auth);
        return requestHead;
    }



}
